using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Shop.Application.UserAdmin;
using Shop.DB;
using Stripe;

namespace Test4
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor();

            //配置数据上下文并指定连接串
            services.AddDbContext<DBContext>(options =>
            options.UseSqlServer(Configuration["ShopContext"]));

            services.AddIdentity<IdentityUser,IdentityRole>(options =>
            {
                options.Password.RequireDigit = false;
                options.Password.RequiredLength = 6;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireLowercase = false;
                options.Password.RequireUppercase = false;
            }).AddEntityFrameworkStores<DBContext>();

            services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = "/Account/Index";
            });

            services.AddAuthorization(options =>
            {
                options.AddPolicy("Admin", policy => policy.RequireClaim("Role","Admin"));
                //options.AddPolicy("Manger", policy => policy.RequireClaim("Role","Manger"));
                options.AddPolicy("Manger", policy => policy
                    .RequireAssertion(context=>context.User.HasClaim("Role","Manger") || context.User.HasClaim("Role","Admin")));
            });

            services.AddControllersWithViews().AddRazorPagesOptions(options=>
            {
                options.Conventions.AuthorizeAreaFolder("","/admin");
                options.Conventions.AuthorizePage("Users", "Admin");
            }).AddNewtonsoftJson();

            services.AddSession(options =>
            {
                options.Cookie.Name = "Cart";
                options.Cookie.MaxAge = TimeSpan.FromMinutes(30);
            });

            services.AddTransient<CreateUser>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();

            //配置静态文件的读取路径
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot"))
            }
              );

            app.UseRouting();
            app.UseSession();
            app.UseAuthorization();
            app.UseAuthentication();

            StripeConfiguration.ApiKey = Configuration.GetSection("Stripe")["SecretKey"];

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
